package com.woniuxy.cloud.carl.carlgoods.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.cloud.carl.carlgoods.client.SkuQueryClient;
import com.woniuxy.cloud.carl.carlgoods.client.dto.SkuDto;
import com.woniuxy.cloud.carl.carlgoods.client.dto.SpuDto;
import com.woniuxy.cloud.carl.carlgoods.client.param.FindSkuByContionParam;
import com.woniuxy.cloud.carl.carlgoods.client.util.MyPage;
import com.woniuxy.cloud.carl.carlgoods.infra.mapper.SkuMapper;
import com.woniuxy.cloud.carl.carlgoods.infra.model.Sku;
import com.woniuxy.cloud.carl.carlgoods.infra.model.Spu;
import com.woniuxy.result.Result;
import com.woniuxy.utils.Assert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@RestController
@Slf4j
@CrossOrigin("*")
public class SkuQueryImpl implements SkuQueryClient {
    @Resource
    private SkuMapper skuMapper;
    @Override
    public Result<MyPage<SkuDto>>findSkuBycondition(FindSkuByContionParam param) {
        if(param.getPageNum()==null||param.getPageSize()==null){
            param.setPageNum(1);
            param.setPageSize(3);
        }
        Page<Sku> page = new Page<>(param.getPageNum(), param.getPageSize());
        MyPage<SkuDto> myPage= new MyPage<>();
        if(param.getCondition()==null||param.getCondition().equals("")){

            skuMapper.selectPage(page,null);
        }else {

            QueryWrapper<Sku> queryWrapper = new QueryWrapper<>();
            queryWrapper.like("sku_name",param.getCondition())
                    .or().eq("spu_id",param.getCondition());
            skuMapper.selectPage(page,queryWrapper);
        }
        List<SkuDto> spuDtos = new ArrayList<>();

        for (Sku sku : page.getRecords()) {

            SkuDto skuDto = BeanUtil.toBean(sku, SkuDto.class);
            if(skuDto.getSkuStatus()==1){
                skuDto.setSkuStatusName("上架");
            }else {
                skuDto.setSkuStatusName("下架");
            }
            spuDtos.add(skuDto);
        }
        myPage.setPages(page.getPages());
        myPage.setPageNum(page.getCurrent());
        myPage.setTatol(page.getTotal());
        myPage.setPageSize(page.getSize());
        myPage.setList(spuDtos);

        return Result.success(myPage);
    }

    @Override
    public Result<SkuDto> findSkuById(Long skuId) {
        Sku sku = skuMapper.selectById(skuId);
        Assert.state(sku!=null,"该商品不存在");
        SkuDto skuDto = BeanUtil.toBean(sku, SkuDto.class);
        return Result.success(skuDto);
    }

    @Override
    public Result<List<SkuDto>> findSkuBySpuId(Long spuId) {
        QueryWrapper<Sku> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("spu_id",spuId);
        List<Sku> skus = skuMapper.selectList(queryWrapper);
        List<SkuDto> skuDtos = BeanUtil.copyToList(skus, SkuDto.class);
        return Result.success(skuDtos);
    }

}
